home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / CURTOOL.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  272 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "curtool.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TTM_GETCURRENTTOOL"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static TOOLINFO ti;
  111. static int      index;
  112.  
  113. static HWND     hToolTip = NULL;
  114.  
  115.    switch( uMsg )
  116.    {
  117.       case WM_CREATE :
  118.               {
  119.                  InitCommonControls();
  120.  
  121.                  // Create tool tip control.
  122.                  //.........................
  123.                  hToolTip = CreateWindowEx( 0,
  124.                                             TOOLTIPS_CLASS, NULL, 
  125.                                             TTS_ALWAYSTIP,
  126.                                             CW_USEDEFAULT, 
  127.                                             CW_USEDEFAULT, 
  128.                                             CW_USEDEFAULT, 
  129.                                             CW_USEDEFAULT,
  130.                                             hWnd, NULL, 
  131.                                             hInst, NULL );
  132.  
  133.                  // Add the tools to the tool tip control.
  134.                  //.......................................
  135.                  ti.cbSize = sizeof( TOOLINFO );
  136.                  ti.hwnd   = hWnd;
  137.                  ti.hinst  = NULL;
  138.                  ti.uFlags = TTF_SUBCLASS;
  139.  
  140.                  for( index=0; index < 4; index++ )
  141.                  {
  142.                     ti.uId      = index;
  143.                     ti.lpszText = LPSTR_TEXTCALLBACK;
  144.  
  145.                     ti.rect.top    = 0;
  146.                     ti.rect.bottom = 10;
  147.                     ti.rect.left   = 10*index;
  148.                     ti.rect.right  = 10*(index+1);
  149.  
  150.                     SendMessage( hToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti );
  151.                  }
  152.               }
  153.               break;
  154.  
  155.       case WM_SIZE :
  156.               {
  157.                  // Adjust the tool rectangles for the new
  158.                  // section size.
  159.                  //.......................................
  160.                  RECT     rect;
  161.  
  162.                  index = 0;
  163.  
  164.                  ti.cbSize = sizeof( TOOLINFO );
  165.  
  166.                  GetClientRect( hWnd, &rect );
  167.  
  168.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  169.                          index, (LPARAM)&ti ) )
  170.                  {
  171.                     ti.rect.top    = 0;
  172.                     ti.rect.bottom = HIWORD( lParam );
  173.                     ti.rect.left   = (rect.right/4)*index;
  174.                     ti.rect.right  = (rect.right/4)*(index+1);
  175.  
  176.                     SendMessage( hToolTip, TTM_SETTOOLINFO, index, (LPARAM)&ti );
  177.  
  178.                     index++;
  179.                  }
  180.               }
  181.               break;
  182.  
  183.       case WM_PAINT  :
  184.               {
  185.                  // Paint the vertical lines to denote the sections.
  186.                  //.................................................
  187.                  PAINTSTRUCT ps;
  188.  
  189.                  index = 0;
  190.  
  191.                  BeginPaint( hWnd, &ps );
  192.  
  193.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  194.                          index, (LPARAM)&ti ) )
  195.                  {
  196.                     MoveToEx( ps.hdc, ti.rect.right, ti.rect.top, NULL );
  197.                     LineTo( ps.hdc, ti.rect.right, ti.rect.bottom );
  198.                     index++;
  199.                  }
  200.  
  201.                  EndPaint( hWnd, &ps );
  202.               }
  203.               break;
  204.  
  205.       case WM_NOTIFY :
  206.               switch ( ((LPNMHDR)lParam)->code )
  207.               {
  208.                  case TTN_NEEDTEXT :
  209.                       wsprintf( ((LPTOOLTIPTEXT)lParam)->szText, 
  210.                                 "Section %d", 
  211.                                 ((LPTOOLTIPTEXT)lParam)->hdr.idFrom+1 );
  212.                       break;
  213.  
  214.                  case TTN_SHOW :
  215.                       ti.cbSize = sizeof( TOOLINFO );
  216.                       SendMessage( hToolTip, TTM_GETCURRENTTOOL, 0, (LPARAM)&ti );
  217.  
  218.                       // Use the tool information
  219.                       
  220.                       break;
  221.               }
  222.               break;
  223.  
  224.  
  225.       case WM_COMMAND :
  226.               switch( LOWORD( wParam ) )
  227.               {
  228.                  case IDM_ABOUT :
  229.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  230.                         break;
  231.  
  232.                  case IDM_EXIT :
  233.                         DestroyWindow( hWnd );
  234.                         break;
  235.               }
  236.               break;
  237.       
  238.       case WM_DESTROY :
  239.               PostQuitMessage(0);
  240.               break;
  241.  
  242.       default :
  243.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  244.    }
  245.  
  246.    return( 0L );
  247. }
  248.  
  249.  
  250. LRESULT CALLBACK About( HWND hDlg,           
  251.                         UINT message,        
  252.                         WPARAM wParam,       
  253.                         LPARAM lParam)
  254. {
  255.    switch (message) 
  256.    {
  257.        case WM_INITDIALOG: 
  258.                return (TRUE);
  259.  
  260.        case WM_COMMAND:                              
  261.                if (   LOWORD(wParam) == IDOK         
  262.                    || LOWORD(wParam) == IDCANCEL)    
  263.                {
  264.                        EndDialog(hDlg, TRUE);        
  265.                        return (TRUE);
  266.                }
  267.                break;
  268.    }
  269.  
  270.    return (FALSE); 
  271. }
  272.